<?php

namespace trade\common\gather;

use linslin\yii2\curl;

class GatherAliyun extends Gather
{
    //public static $appcode = 'B1016FC0B8124FD38C558DC79689C18D';
//    public static $appcode = '94463dd6a31b45779c17832c4d3992e9';
    public static $appcode = '3454CE59DA4541DE84A4AC7E01EC759A';

    public function run($tables, $prex = 'trade')
    {
        $this->loadSymbol($tables, function ($items) use ($prex) {
            $code = [];
            foreach ($items as $item) {
                $code[] = $item['code2'];
            }
            $tickets = $this->ticekts($code);
            if ($tickets) {
                foreach ($items as $item) {
                    if (!empty($tickets[$item['code2']])) {
                        $item['ticket'] = $tickets[$item['code2']];
                        $item['ticket']['code'] = $item['code'];
                        $this->_listen($item, $prex);
                    }
//                    else {
//                        echo $item['code2'] . ' not gather' . PHP_EOL;
//                        // Yii::$app->db->createCommand("UPDATE `rf_addon_tf_futures_symbol` SET `on_sale`='off' WHERE `code2` = '{$item['code']}'")->execute();
//                        //echo $item['code'].' not gather'.PHP_EOL;
//                    }
                }
            }
        }, 10);
    }


    public function formset_tickt($symbol)
    {
        return array_merge(self::$format, [
            'ask' => $symbol['S1'],
            'asm' => $symbol['S1V'],
            'bid' => $symbol['B1'],
            'bim' => $symbol['B1V'],
            'ask2' => $symbol['S2'],
            'asm2' => $symbol['S2V'],
            'bid2' => $symbol['B2'],
            'bim2' => $symbol['B2V'],
            'ask3' => $symbol['S3'],
            'asm3' => $symbol['S3V'],
            'bid3' => $symbol['B3'],
            'bim3' => $symbol['B3V'],
            'ask4' => $symbol['S4'],
            'asm4' => $symbol['S4V'],
            'bid4' => $symbol['B4'],
            'bim4' => $symbol['B4V'],
            'ask5' => $symbol['S5'],
            'asm5' => $symbol['S5V'],
            'bid5' => $symbol['B5'],
            'bim5' => $symbol['B5V'],
            'open' => $symbol['O'],
            'close' => $symbol['YC'],
            'js'    => $symbol['JS'],
            'nv'    => $symbol['NV'],
            'high' => $symbol['H'],
            'low' => $symbol['L'],
            'wave' => $symbol['ZF'],
            'price' => $symbol['P'],
            'volume' => $symbol['NV'],
            'position' => $symbol['HD'],
            'ctm' => $symbol['Tick'],
            'ctmfmt' => date('Y-m-d H:i:s', $symbol['Tick'])
        ]);
    }

    public function ticekts($symbol)
    {
        if (is_array($symbol)) {
            $symbol = implode(',', $symbol);
        }
        $curl = new curl\Curl();
        $curl = $curl->setHeaders(['Authorization' => 'APPCODE ' . self::$appcode]);
        //$resp = $curl->get("http://map.konpn.com:10002/localrm/query/comrms?symbols={$symbol}");

        $resp = $curl->get("http://map.konpn.com:10002/localrm/query/comrms?symbols={$symbol}");
        if ($curl->errorCode == null) {

            $resp = json_decode($resp, true);
            if ($resp && !empty($resp['Obj'])) {
                foreach ($resp['Obj'] as $_symbol) {

                    $resp['tmplist'][$_symbol['FS']] = $this->formset_tickt($_symbol);

                }
                return $resp['tmplist'];
            }
        }
        return false;
    }

}
